<template>
  <div class="main-app">
    <el-card class="box-card">
      <el-form :inline="true" :model="formInline" class="form-inline">
        <el-form-item label="转运申请编号:">
          <el-input v-model="formInline.number" clearable />
        </el-form-item>
        <el-form-item label="申请时间:">
          <div class="block">
            <el-date-picker v-model="formInline.transportTime" type="daterange" range-separator="至"
                            start-placeholder="开始日期" end-placeholder="结束日期" style="width: 100%;" format="yyyy 年 MM 月 dd 日"
            />
          </div>
        </el-form-item>

        <!-- <el-form-item label="状态:">
          <el-select v-model="formInline.SingleCopyStatus" placeholder="请选择" clearable @change="selectedState">
            <el-option v-for="item in transportApplyStateOption" :key="item.value" :label="item.label"
                       :value="item.value"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="search">
            查询
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-refresh-right" @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <div class="show-result">
      <el-form :inline="true" class="form-inline">

        <el-form-item>
          <el-button icon="el-icon-arrow-left" @click="cancel">返 回</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-plus" :disabled="disabled" @click="selectedApply">
            选 择
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-view" :disabled="disabled" @click="see">
            查看申请
          </el-button>
        </el-form-item>
      </el-form>

      <div class="result" style="margin-top: -10px;">
        <el-table id="table" ref="multipleTable" v-loading="loading" :data="tableData" border tooltip-effect="dark"
                  style="width: 100%" @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column prop="transferPlanNumber" label="转运计划编号" />
          <el-table-column prop="number" label="转运申请编号" />
          <el-table-column prop="wpcName" label="产废单位名称" width="300" />
          <el-table-column prop="ccName" label="接受单位名称" width="300" />
          <el-table-column prop="wastWeight" label="拟定废物重量 (吨)" />
          <el-table-column prop="type" label="状态" width="120">
            <template slot-scope="scope">
              <el-tag style="width: 100%; text-align: center;" effect="dark">{{ scope.row.type | parseType }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="申请时间">
            <template slot-scope="scope">
              {{ scope.row.createTime | parseDate }}
            </template>
          </el-table-column>
        </el-table>
        <div class="page">
          <el-pagination background :current-page.sync="currentPage" layout="total,prev, pager, next,sizes"
                         :total="total" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" @current-change="handleCurrentChange"
                         @size-change="handleSizeChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// Table组件相关逻辑，需要引用
import {
  pageMixin
} from "../../../mixins/index";
import {
  transportApplyState,
  CarType
} from "../../../utils/resource.js";
import {
  parseTime
} from '../../../utils/index.js';
import {
  getTransportApplyList,
} from '../../../api/wastereceipt.js';
export default {
  filters: {
    parseDate(value) {
      if (value) {
        return value.replace("T", " ");
      }
    },
    parseType(value) {
      if (value != null) {
        return transportApplyState.find((p) => p.value === `${value}`).label;
      }
    },

  },
  mixins: [pageMixin],

  data() {
    return {
      transportApplyStateOption: transportApplyState,
      formInline: {
        transportTime: ["", ""],
        transferPlanNumber: "",
        SingleCopyStatus: "40,50",
      },
      tempInhouseObjInfo_Receive_ACC: {},

    };
  },
  async created() {
    await this.init();
  },
  methods: {
    async init() {
      this.tempInhouseObjInfo_Receive_ACC = JSON.parse(localStorage.getItem('tempInhouseObjInfo_Receive_ACC'));
      if (!this.tempInhouseObjInfo_Receive_ACC) {
        this.tempInhouseObjInfo_Receive_ACC.applyObj = {};
      }

      this.tableData = [];
      this.loading = true;

      const {
        data,
        count
      } = await getTransportApplyList({
        limit: this.pageSize,
        page: this.currentPage,
        searchParam: JSON.stringify({
          TransferPlanNumber: this.formInline.transferPlanNumber,
          Number: this.formInline.number,
          SeriesId: "eLabR_Middleground",
          Type: this.formInline.SingleCopyStatus,
          StartTime: parseTime(this.formInline.transportTime[0]) === null ? "" : parseTime(this.formInline
            .transportTime[0]).substring(0, 10),
          EndTime: parseTime(this.formInline.transportTime[1]) === null ? "" : parseTime(this.formInline
            .transportTime[1]).substring(0, 10),
			 AreaNumber:  this.$store.state.user.userObj.areaNumberStr,
        })
      });
      this.tableData = data;
      console.log("得到的转移申请List：", data);
      this.total = count;
      this.loading = false;
    },

    selectedState(val) {
      console.log(val);
    },
    async search() {
      await this.init();
    },
    async reset() {
      this.formInline = {
        transferPlanNumber: "",
        code: "",
        transportTime: "",
        SingleCopyStatus: "40,50",
      };
      await this.init();
    },

    // handleSizeChange(val) {},
    // handleCurrentChange(val) {},
    // handleSelectionChange() {},
    handleSelectionChange(val) {
      if (val.length !== 1) {
        this.disabled = true;
      } else {
        this.disabled = false;
        this.id = val[0].id;
      }
      this.tempInhouseObjInfo_Receive_ACC.applyObj = val[0]; // 选中的一行数据
      this.tempInhouseObjInfo_Receive_ACC.applyId = val[0].id; // 选中的一行数据
    },

    selectedApply(val) {
      var obj = CarType.find((p) => p.value === `${this.tempInhouseObjInfo_Receive_ACC.applyObj.vehicleModel}`);
      if (obj) {
        this.tempInhouseObjInfo_Receive_ACC.applyObj.vehicleModel = obj.label;
      }
      localStorage.setItem('tempInhouseObjInfo_Receive_ACC', JSON.stringify(this.tempInhouseObjInfo_Receive_ACC));
      this.$router.push("/warehouse/inhouse_receive");
    },

    see() {
      this.$router.push(`/warehouse/inhouse_receive/applyDetail/${this.id}`);
    },
    cancel() {
      this.$router.push("/warehouse/inhouse_receive");
    },

  },
};
</script>

<style lang="scss" scoped>
  .result {
    margin-top: 10px;
  }

  .page {
    margin-top: 20px;
    text-align: right;
    margin-bottom: 40px;
  }

  .show-result {
    margin-top: 10px;
  }
</style>
